/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.core.commands;

import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.RequestConstants;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.requests.ChangeBoundsRequest;

import com.cea.papyrus.core.commands.connection.MoveRectangleAnchorsCommand;
import com.cea.papyrus.diagraminterchange2.di2.GraphNode;



/**
 * A command to resize and/or move a shape.
 * The command can be undone or redone.
 * 
 * @author Elias Volanakis
 */
public class GraphNodeSetConstraintCommand extends Command {


	/**
	 * Stores the new size and location.
	 */
	private final Rectangle newBounds;
	
	/**
	 * Stores the old size and location.
	 */
	private Rectangle oldBounds;
	
	/**
	 * A request to move/resize an edit part.
	 */
	private final ChangeBoundsRequest request;
	
	/**
	 * Save state of the associated anchors.
	 */
	private MoveRectangleAnchorsCommand oldAnchors;

	/**
	 * Shape to manipulate.
	 */
	private final GraphNode shape;

	/**
	 * Create a command that can resize and/or move a shape.
	 * 
	 * @param newBounds the new size and location
	 * @param shape the shape to manipulate
	 * @param req 	the move and resize request
	 * 
	 * @throws IllegalArgumentException if any of the parameters is null
	 */
	public GraphNodeSetConstraintCommand(GraphNode shape, ChangeBoundsRequest req, Rectangle newBounds) {
		if (shape == null || req == null || newBounds == null) {
			throw new IllegalArgumentException();
		}
		this.shape = shape;
		this.request = req;
		this.newBounds = newBounds.getCopy();
		setLabel(computeLabel());
	}

	/**
	 * Compute the label to be set in the menu.
	 * 
	 * @return 
	 */
	protected String computeLabel()
	{
		Object type = request.getType();
		if(RequestConstants.REQ_MOVE.equals(type)
				|| RequestConstants.REQ_MOVE_CHILDREN.equals(type) )
		{
			return "move";
		}
		else if( RequestConstants.REQ_RESIZE.equals(type)
				|| RequestConstants.REQ_RESIZE_CHILDREN.equals(type))
		{
			return "resize";
		}
		else
		{
			return "move / resize";
		}

	}

	/* (non-Javadoc)
	 * @see org.eclipse.gef.commands.Command#canExecute()
	 */
	/**
	 * 
	 * 
	 * @return 
	 */
	public boolean canExecute() {

		return true;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.gef.commands.Command#execute()
	 */
	/**
	 * 
	 */
	public void execute() {
		oldBounds = new Rectangle(shape.getPosition(), shape.getSize());
		oldAnchors = new MoveRectangleAnchorsCommand(shape);
		oldAnchors.savePositions();
		redo();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.gef.commands.Command#redo()
	 */
	/**
	 * 
	 */
	public void redo() {
		shape.setSize(newBounds.getSize());
		shape.setPosition(newBounds.getLocation());
		oldAnchors.moveAttachedAnchors(newBounds, oldBounds);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.gef.commands.Command#undo()
	 */
	/**
	 * 
	 */
	public void undo() {
		shape.setSize(oldBounds.getSize());
		shape.setPosition(oldBounds.getLocation());
		oldAnchors.restorePositions();
	}

}
